<?php

namespace iBrand\Shop\Backend\Console;

use Illuminate\Console\Command;
use DB;

class PurchaseBackendMenusCommand extends Command
{
	protected $signature = 'import:purchase-backend-menus';

	protected $description = 'import purchase backend menus';

	public function handle()
	{
		$lastOrder       = DB::table(config('admin.database.menu_table'))->max('order');
		$purchaseTopMenu = DB::table(config('admin.database.menu_table'))->where('title', '仓库管理')->where('parent_id', 0)->first();
		if (!$purchaseTopMenu) {
			$purchaseTopMenuId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => 0,
				'order'      => $lastOrder++,
				'title'      => '仓库管理',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 1,
				'uri'        => 'warehouse/purchase',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$purchaseTopMenuId = $purchaseTopMenu->id;
		}

		$purchaseManager = DB::table(config('admin.database.menu_table'))->where('title', '采购管理')->where('parent_id', $purchaseTopMenuId)->first();
		if (!$purchaseManager) {
			$purchaseManagerId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $purchaseTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '采购管理',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$purchaseManagerId = $purchaseManager->id;
		}

		$purchase = DB::table(config('admin.database.menu_table'))->where('title', '采购单')->where('parent_id', $purchaseManagerId)->first();
		if (!$purchase) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $purchaseManagerId,
				'order'      => $lastOrder++,
				'title'      => '采购单',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'warehouse/purchase',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$purchaseRefund = DB::table(config('admin.database.menu_table'))->where('title', '采购退货单')->where('parent_id', $purchaseManagerId)->first();
		if (!$purchaseRefund) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $purchaseManagerId,
				'order'      => $lastOrder++,
				'title'      => '采购退货单',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'warehouse/reject',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$transfer = DB::table(config('admin.database.menu_table'))->where('title', '调拨单')->where('parent_id', $purchaseManagerId)->first();
		if (!$transfer) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $purchaseManagerId,
				'order'      => $lastOrder++,
				'title'      => '调拨单',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'warehouse/transfer',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$stockManager = DB::table(config('admin.database.menu_table'))->where('title', '库存管理')->where('parent_id', $purchaseTopMenuId)->first();
		if (!$stockManager) {
			$stockManagerId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $purchaseTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '库存管理',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => '',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$stockManagerId = $stockManager->id;
		}

		$stockQuery = DB::table(config('admin.database.menu_table'))->where('title', '库存查询')->where('parent_id', $stockManagerId)->first();
		if (!$stockQuery) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $stockManagerId,
				'order'      => $lastOrder++,
				'title'      => '库存查询',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'warehouse/stock/query',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$stockStatistics = DB::table(config('admin.database.menu_table'))->where('title', '库存盘点')->where('parent_id', $stockManagerId)->first();
		if (!$stockStatistics) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $stockManagerId,
				'order'      => $lastOrder++,
				'title'      => '库存盘点',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'warehouse/take/stock/plan',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$stockRecord = DB::table(config('admin.database.menu_table'))->where('title', '库存流水')->where('parent_id', $stockManagerId)->first();
		if (!$stockRecord) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $stockManagerId,
				'order'      => $lastOrder++,
				'title'      => '库存流水',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'warehouse/record',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		/*
		 * 报表：
		 * 采购报表、采购退货报表、销售报表、盘点报表、调拨报表
		 * */
		$ReportTopMenu = DB::table(config('admin.database.menu_table'))->where('title', '报表')->where('parent_id', 0)->first();
		if (!$ReportTopMenu) {
			$ReportTopMenuId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => 0,
				'order'      => $lastOrder++,
				'title'      => '报表',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'report/purchase',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$ReportTopMenuId = $ReportTopMenu->id;
		}

		$purchaseReport = DB::table(config('admin.database.menu_table'))->where('title', '采购报表')->where('parent_id', $ReportTopMenuId)->first();
		if (!$purchaseReport) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $ReportTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '采购报表',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'report/purchase',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$saleReport = DB::table(config('admin.database.menu_table'))->where('title', '销售报表')->where('parent_id', $ReportTopMenuId)->first();
		if (!$saleReport) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $ReportTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '销售报表',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'report/sales',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$storeReport = DB::table(config('admin.database.menu_table'))->where('title', '盘点报表')->where('parent_id', $ReportTopMenuId)->first();
		if (!$storeReport) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $ReportTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '盘点报表',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'report/stock',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$transferReport = DB::table(config('admin.database.menu_table'))->where('title', '调拨报表')->where('parent_id', $ReportTopMenuId)->first();
		if (!$transferReport) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $ReportTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '调拨报表',
				'icon'       => 'iconfont icon-mendianguanli',
				'blank'      => 0,
				'uri'        => 'report/allot',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}
	}
}